package command

import (
	"encoding/hex"
	"encoding/json"
	"gdfs-center/db"
	"gdfs-center/socket/codec"
	"log"
)

func AnalysisUploadSysInfo(data *codec.DataDockPackage) {
	var sysinfo codec.UploadHardWareInfo
	hexbyte, _ := hex.DecodeString(data.Data)
	jsonerr := json.Unmarshal(hexbyte, &sysinfo)
	if jsonerr != nil {
		log.Println("###json err:", jsonerr)
	}
	var sinfo db.SystemInfo
	db.GetDB().Where("store_id = ?", sysinfo.StoreId).First(&sinfo)
	if sinfo == (db.SystemInfo{}) {
		//创建
		sinfo.StoreId = sysinfo.StoreId
		sinfo.CpuPercent = sysinfo.CpuPercent
		sinfo.CpuGhz = sysinfo.CpuGhz
		sinfo.CpuCount = sysinfo.CpuCount
		sinfo.MemUsed = sysinfo.MemUsed
		sinfo.MemTotal = sysinfo.MemTotal
		sinfo.MemPercent = sysinfo.MemPercent
		sinfo.DiskPercent = sysinfo.DiskPercent
		sinfo.DiskUsed = sysinfo.DiskUsed
		sinfo.DiskTotal = sysinfo.DiskTotal
		sinfo.SwapPercent = sysinfo.SwapPercent
		sinfo.SwapUsed = sysinfo.SwapUsed
		sinfo.SwapTotal = sysinfo.SwapTotal
		sinfo.StoreFileCount = sysinfo.StoreFileCount
		db.GetDB().Create(&sinfo)
	} else {
		//更新
		sinfo.CpuGhz = sysinfo.CpuGhz
		sinfo.CpuCount = sysinfo.CpuCount
		sinfo.CpuPercent = sysinfo.CpuPercent
		sinfo.MemUsed = sysinfo.MemUsed
		sinfo.MemTotal = sysinfo.MemTotal
		sinfo.MemPercent = sysinfo.MemPercent
		sinfo.DiskPercent = sysinfo.DiskPercent
		sinfo.DiskUsed = sysinfo.DiskUsed
		sinfo.DiskTotal = sysinfo.DiskTotal
		sinfo.SwapPercent = sysinfo.SwapPercent
		sinfo.SwapUsed = sysinfo.SwapUsed
		sinfo.SwapTotal = sysinfo.SwapTotal
		sinfo.StoreFileCount = sysinfo.StoreFileCount
		db.GetDB().Save(&sinfo)
	}

}
